package com.time.auth.application.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.base.Preconditions;
import com.time.auth.application.convent.AuthRolePermissionConverter;
import com.time.auth.application.dto.AuthRolePermissionDTO;
import com.time.auth.domain.entity.AuthRolePermissionBO;
import com.time.auth.domain.service.AuthRolePermissionDomainService;
import com.time.auth.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 角色权限关联控制层类
 *
 * @author: HuangQi
 * @date: Created in 16:33 2025/9/28
 * @description: 角色权限关联控制层类
 */
@Slf4j
@RestController
@RequestMapping("/rolePermission/")
public class RolePermissionController {

    @Autowired
    private AuthRolePermissionDomainService authRolePermissionDomainService;

    /**
     * 新增角色权限关联
     *
     * @param authRolePermissionDTO 新增角色权限关联的信息
     * @return 新增结果
     */
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody AuthRolePermissionDTO authRolePermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RolePermissionController.add.dto:{}", authRolePermissionDTO);
            }

            // 参数验证
            Preconditions.checkArgument(!CollectionUtils.isEmpty(authRolePermissionDTO.getPermissionIdList()), "权限关联不能为空");
            Preconditions.checkNotNull(authRolePermissionDTO.getRoleId(),"角色ID不能为空");

            // 调用领域层
            AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionConverter.INSTANCE.convertDTOToBo(authRolePermissionDTO);
            Boolean addResult = authRolePermissionDomainService.add(authRolePermissionBO);

            // 返回结果
            return Result.ok(addResult);
        }catch (Exception e){
            // 异常处理
            log.error("RolePermissionController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增角色权限关联失败");
        }

    }
}
